package com.yangjunli.bishe.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangjunli.bishe.core.RequestHolder;
import com.yangjunli.bishe.core.ReturnT;
import com.yangjunli.bishe.entity.User;
import com.yangjunli.bishe.mapper.UserMapper;
import com.yangjunli.bishe.service.IUserService;
import com.yangjunli.bishe.utils.tokenUtils;
import com.yangjunli.bishe.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 前端控制器  用户表
 * </p>
 *
 * @author jobob
 * @since 2020-04-04
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    private static final String base64Security = "1234567879";

    @Autowired
    IUserService userServiceImpl;

    @Resource
    UserMapper userMapper;



    @PostMapping("/login/admin")
    public ReturnT loginAdmin(@RequestBody User userReq,HttpServletRequest request, HttpServletResponse response) {
        try {
            if(!"admin".equals(userReq.getUsername())){
                return new ReturnT(100, "非管理员账号");
            }
            User user = userMapper.querBynamePassword(userReq);
            if (user == null) {
                return new ReturnT(100, "用户名或密码不正确");
            } else {
                request.getSession().setAttribute("userid",user.getId());
                return new ReturnT(user);
            }
        }catch (Exception e){
            log.error("login",e);
            return new ReturnT(100, "登录失败");
        }
    }



    @PostMapping("/login")
    public ReturnT login(@RequestBody User userReq,HttpServletRequest request, HttpServletResponse response) {

        try {
            User user = userMapper.querBynamePassword(userReq);
            if (user == null) {
                return new ReturnT(100, "用户名或密码不正确");
            } else {
                request.getSession().setAttribute("userid",user.getId());
                return new ReturnT(user);
           }
        }catch (Exception e){
            log.error("login",e);
            return new ReturnT(100, "登录失败");
        }
    }

    @GetMapping("/loginOut")
    public ReturnT loginOut(HttpServletRequest request, HttpServletResponse response) {
        try {
            //HttpSession session = RequestHolder.getCurrentRequest().getSession();
            //session.removeAttribute("id");
            return new ReturnT(200, "退出成功");
        }catch (Exception e){
            log.error("login",e);
            return new ReturnT(100, "退出失败");
        }
    }

    @PostMapping("/regist")
    public ReturnT regist(@RequestBody UserVo UserVo) {
        try {
            String twopassword = UserVo.getTwopassword();
            String password = UserVo.getPassword();
            UserVo.setName("小明1 号");
            if(!password.equals(twopassword)){
                return new ReturnT(100, "两次密码不正确");
            }

            User userReq = new User();
            BeanUtils.copyProperties(UserVo,userReq);
            //用户名是否重复
            User user1 = userMapper.queryByUserName(userReq);
            if (user1 != null) {
                return new ReturnT(100, "用户名重复");
            }
            userReq.setCreateDate(LocalDateTime.now());
            userServiceImpl.save(userReq);
        } catch (Exception e) {
            log.error("regist",e);
            return new ReturnT(100, "注册失败");
        }
        return new ReturnT(200, "注册成功");
    }

    @GetMapping("/userName/{userName}")
    public ReturnT getUser(@PathVariable("userName") String userName){

        try{
            User user = new User();
            user.setUsername(userName);
            user = userMapper.queryByUserName(user);
            return new ReturnT(user);
        }catch (Exception e){
            log.error("getUser",e);
            return new ReturnT(100, "注册失败");
        }
    }

    @GetMapping("/id")
    public ReturnT getUserById(){
        try{
            Object userid = RequestHolder.getCurrentRequest().getSession().getAttribute("userid");

            if(userid == null){
                return new ReturnT(100, "注册失败");
            }

            User user = userMapper.queryByUserId(Integer.valueOf(userid.toString()));
            return new ReturnT(user);
        }catch (Exception e){
            log.error("getUser",e);
            return new ReturnT(100, "注册失败");
        }
    }


    @PutMapping("/updateUser")
    public ReturnT uodateUser(@RequestBody User user){
        try{
            Object userid = RequestHolder.getCurrentRequest().getSession().getAttribute("userid");

            if(userid == null){
                return new ReturnT(100, "未登陆接口异常");
            }
            userServiceImpl.updateById(user);
            return new ReturnT(user);
        }catch (Exception e){
            log.error("getUser",e);
            return new ReturnT(100, "注册失败");
        }
    }


    @GetMapping("/userAll")
    public ReturnT uodateUser(Integer pageNo ,Integer pageSize){
        try{
            IPage<User> page = new Page<>(pageNo,pageSize);
            IPage<User> item = userServiceImpl.page(page);
            return new ReturnT(item);
        }catch (Exception e){
            log.error("getUser",e);
            return new ReturnT(100, "获取失败");
        }
    }


}
